/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.common.utils.i18n;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.dayatang.i18n.I18nService;
import com.dayatang.i18n.NoSuchMessageException;
import com.dayatang.i18n.impl.ResourceBundleI18nService;
import com.dayatang.i18n.support.I18nServiceAccessor;


/**
 * @description
 *
 * @author rening
 * @version v1.0
 *
 * @2013-8-9
 *
 */
public class I18NResource {

	private static final String I18N_PATH = "i18n";

	private static I18NResource resource = null;
	
	private I18nService i18nService;
	
	private I18nServiceAccessor accessor;
	
	private List<String> basenames = new ArrayList<String>();
	
	private void loadResource() {
		URL url = Thread.currentThread().getContextClassLoader().getResource(I18N_PATH);
		if (url != null) {
			File file = new File(url.getFile());
			handleBasename(file, new StringBuffer());
		}
	}
	
	private void handleBasename(File file, StringBuffer sb) {
		if(file.isDirectory()){
			sb.append(file.getName()).append(".");
			File[] files = file.listFiles();
			if(files != null){
				for (File f : files) {
					handleBasename(f, sb);
				}
			}
		}
		
		if (file.getName().lastIndexOf("properties") != -1) {
			if (file.getName().indexOf("_") == -1) {
				sb.append(file.getName().substring(0, file.getName().indexOf(".")));
			} else {
				sb.append(file.getName().substring(0, file.getName().indexOf("_")));
			}
			basenames.add(sb.toString());
		}
	}
	
	private void initI18nService(){
		ResourceBundleI18nService service = new ResourceBundleI18nService();
		service.setBasenames(basenames.toArray(new String[basenames.size()]));
		i18nService = service;
	}
	
	private void initI18nServiceAccessor(){
		accessor = new I18nServiceAccessor(i18nService);
	}
	
	public static I18NResource getResource(){
		synchronized (I18NResource.class) {
			if(resource != null){
				return resource;
			}
			
			resource = new I18NResource();
			resource.loadResource();
			resource.initI18nService();
			resource.initI18nServiceAccessor();
		}
		return resource;
	}
	

	/**
	 * Retrieve the message for the given code and the default Locale.
	 * 
	 * @param code
	 *            code of the message
	 * @param defaultMessage
	 *            String to return if the lookup fails
	 * @return the message
	 */
	public String getMessage(String code, String defaultMessage) {
		return this.accessor.getMessage(code, defaultMessage);
	}

	/**
	 * Retrieve the message for the given code and the given Locale.
	 * 
	 * @param code
	 *            code of the message
	 * @param defaultMessage
	 *            String to return if the lookup fails
	 * @param locale
	 *            Locale in which to do lookup
	 * @return the message
	 */
	public String getMessage(String code, String defaultMessage, Locale locale) {
		return this.accessor.getMessage(code, defaultMessage, locale);
	}

	/**
	 * Retrieve the message for the given code and the default Locale.
	 * 
	 * @param code
	 *            code of the message
	 * @param args
	 *            arguments for the message, or <code>null</code> if none
	 * @param defaultMessage
	 *            String to return if the lookup fails
	 * @return the message
	 */
	public String getMessage(String code, Object[] args, String defaultMessage) {
		return this.accessor.getMessage(code, args, defaultMessage);
	}

	/**
	 * Retrieve the message for the given code and the given Locale.
	 * 
	 * @param code
	 *            code of the message
	 * @param args
	 *            arguments for the message, or <code>null</code> if none
	 * @param defaultMessage
	 *            String to return if the lookup fails
	 * @param locale
	 *            Locale in which to do lookup
	 * @return the message
	 */
	public String getMessage(String code, Object[] args, String defaultMessage,
			Locale locale) {
		return this.accessor.getMessage(code, args, defaultMessage, locale);
	}

	/**
	 * Retrieve the message for the given code and the default Locale.
	 * 
	 * @param code
	 *            code of the message
	 * @return the message
	 * @throws com.dayatang.i18n.NoSuchMessageException
	 *             if not found
	 */
	public String getMessage(String code) throws NoSuchMessageException {
		return this.accessor.getMessage(code);
	}

	/**
	 * Retrieve the message for the given code and the given Locale.
	 * 
	 * @param code
	 *            code of the message
	 * @param locale
	 *            Locale in which to do lookup
	 * @return the message
	 * @throws com.dayatang.i18n.NoSuchMessageException
	 *             if not found
	 */
	public String getMessage(String code, Locale locale)
			throws NoSuchMessageException {
		return this.accessor.getMessage(code, locale);
	}

	/**
	 * Retrieve the message for the given code and the default Locale.
	 * 
	 * @param code
	 *            code of the message
	 * @param args
	 *            arguments for the message, or <code>null</code> if none
	 * @return the message
	 * @throws com.dayatang.i18n.NoSuchMessageException
	 *             if not found
	 */
	public String getMessage(String code, Object[] args)
			throws NoSuchMessageException {
		return this.accessor.getMessage(code, args);
	}

	/**
	 * Retrieve the message for the given code and the given Locale.
	 * 
	 * @param code
	 *            code of the message
	 * @param args
	 *            arguments for the message, or <code>null</code> if none
	 * @param locale
	 *            Locale in which to do lookup
	 * @return the message
	 * @throws com.dayatang.i18n.NoSuchMessageException
	 *             if not found
	 */
	public String getMessage(String code, Object[] args, Locale locale)
			throws NoSuchMessageException {
		return this.accessor.getMessage(code, args, locale);
	}

}
